Adding Viewing records with JSP
Following form is takes three data values: product name, quantity, and unit price of the product from users. Those data values are sent to the store.jsp.
<html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class='container'> <div class='row' style='width:100px;background-color:#cecece'> This is admin page </div> <div class='card'> <div class='card-body'> </div> <form method='post' action = 'store.jsp'> <p><input type='text' name='pname'></p> <p><input type='text' name='quantity'></p> <p><input type='text' name='unitprice'></p> <p><input type='submit' name='submit' id='submit'></p> </form> </div> </div> </body> </html>
This file gets data from the users and stores those data in the database. It makes
<%@page import="java.sql.*"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@page import="java.io.*"%> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class='container'> <div class='row' style='width:100px;background-color:#cecece'> This is admin page </div> <div class='card'> <div class='card-body'> <% Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password="); String pname = request.getParameter("pname"); int quantity = Integer.parseInt(request.getParameter("quantity")); int unitprice = Integer.parseInt(request.getParameter("unitprice")); Statement st=(Statement) c.createStatement(); out.print(st.executeUpdate("insert into product(pname,quantity,unitprice) values('"+pname+"',"+quantity+","+unitprice+")")); out.print("<h4><a href='insertform.html'>Add new Product</a></h4>"); out.print("<h4><a href='viewproduct.jsp'>View Product</a></h4>");
This file name view.jsp displays all the products stored in the database table. Records are displayed in the table. The table has four columns: Product Name, Quantity, Unit Price and Action. In each row, action column has edit text. This edit text is linked to edit.jsp page and product id of the product belonging to that row is passed to edit.jsp. If this edit text is clicked new page will be opened where information of the product with specific id is displayed in the form.
<%@page import="java.sql.*"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@page import="java.io.*"%> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class='container'> <div class='row' style='width:100px;background-color:#cecece'> This is admin page </div> <div class='card'> <div class='card-body'> <% Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password="); Statement st=(Statement) c.createStatement(); ResultSet rs = st.executeQuery("select pid,pname, quantity,unitprice from product order by unitprice"); out.print("<table>"); out.print("<tr><th>Product name</th><th>Quantity</th><th>Unit Price</th><th>Action</th></tr>"); while(rs.next()){ out.print("<tr><td>"+rs.getString("pname")+"</td><td>"+rs.getInt("quantity")+"</td><td>"+rs.getInt("unitprice")+"</td><td><a href='edit.jsp?id="+rs.getInt("pid")+"'>Edit</a></td></tr>"); } c.close(); out.print("</table>"); out.print("<h4><a href='adminpanel.html'>Add new Product</a></h4>"); out.print("<h4><a href='viewproduct.jsp'>View Product</a></h4>"); %> </div> </div> </div> </body> </html>
<%@page import="java.sql.*"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@page import="java.io.*"%> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class='container'> <div class='row' style='width:100px;background-color:#cecece'> This is admin page </div> <div class='card'> <div class='card-body'> <% out.println("ID: "+request.getParameter("id")); int id = Integer.parseInt(request.getParameter("id")); Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password="); Statement st=(Statement) c.createStatement(); ResultSet rs = st.executeQuery("select pname, quantity,unitprice from product where pid="+id); out.print("<form>"); while(rs.next()){ out.print("<p><input type='text' name='pname' value='"+rs.getString("pname")+"'></p>"); out.print("<p><input type='text' name='quantity' value='"+rs.getString("quantity")+"'></p>"); out.print("<p><input type='text' name='unitprice' value='"+rs.getString("unitprice")+"'></p>"); } c.close(); out.print("</form>"); out.print("<h4><a href='adminpanel.html'>Add new Product</a></h4>"); out.print("<h4><a href='viewproduct.jsp'>View Product</a></h4>"); %> </div> </div> </div> </body> </html>